package com.oocl.chat.ui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Date;

import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.border.Border;
import javax.swing.border.LineBorder;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import com.oocl.chat.controller.Client;
import com.oocl.chat.pojo.Message;
import com.oocl.chat.util.Constans;

public class ChatFrame extends JFrame implements ActionListener{
	DefaultListModel<String> users = new DefaultListModel<String>();
	DefaultListModel<String> msgs = new DefaultListModel<String>();
	private JScrollPane msgScrol;
	private JScrollPane userScrol;
	private JList<String> userList;
	private JList<String> msgList;
	private JTextField sendTf;
	private JButton sendBtn;
	private JButton shakeBtn;
	private String name;
	private String reciever ="所有人";
	private JScrollBar msgJBar;
	private JScrollBar userJBar;
	
	public ChatFrame(String name) {
		this.setTitle("欢迎登陆："+name+"	"+"正在和所有人聊天...");
		this.name = name;
		this.setSize(600, 500);
		this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);	//设置关闭取消操作没动作
		this.setLocationRelativeTo(null);
		init();
		addEvent();
		this.setVisible(true);
	}
	private void init(){
		this.setLayout(new BorderLayout());
		JPanel panLeft=new JPanel(new BorderLayout());
		JPanel panRight=new JPanel(new BorderLayout());
		JPanel panLeftBottom=new JPanel(new BorderLayout());
		JPanel panBtnGroup=new JPanel(new BorderLayout());
		userList = new JList<String>(users);
		userScrol = new JScrollPane(userList);
		userJBar = userScrol.getVerticalScrollBar();
		userJBar.setEnabled(true);
		msgList = new JList<String>(msgs);
		msgScrol = new JScrollPane(msgList);
		msgJBar = msgScrol.getVerticalScrollBar();
		msgJBar.setEnabled(true);
		msgs.addElement("欢迎登陆！！！！");
		users.addElement("所有人");
		userList.setPreferredSize(new Dimension(100,400));
		
		sendTf=new JTextField();
		sendBtn=new JButton("send");
		shakeBtn=new JButton("shake");
		
		sendTf.setFont(new Font("宋体", Font.BOLD, 18));
		sendTf.setForeground(Color.red);
		
		panLeft.add(new JScrollPane(msgScrol), BorderLayout.CENTER);
		
		
		panLeft.add(panLeftBottom,BorderLayout.SOUTH);
		panRight.add(new JScrollPane(userList));
		this.add(panLeft,BorderLayout.CENTER);
		this.add(panRight,BorderLayout.EAST);
		panBtnGroup.add(shakeBtn,BorderLayout.NORTH);
		panBtnGroup.add(sendBtn,BorderLayout.SOUTH);
		panLeftBottom.add(sendTf,BorderLayout.CENTER);
		panLeftBottom.add(panBtnGroup,BorderLayout.EAST);
	}
	
	private void addEvent(){
		sendBtn.addActionListener(this);
		shakeBtn.addActionListener(this);
		sendTf.addActionListener(this);
		this.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				int option = JOptionPane.showConfirmDialog(null, "Exit the system？",
						"Exit Reminder", JOptionPane.YES_NO_OPTION,
						JOptionPane.QUESTION_MESSAGE);
				if (option == JOptionPane.YES_OPTION) {
					Message message = new Message();
					message.setSender(name);
					message.setTime(new Date().getTime());
					message.setType(3);
					Client.client.sendLogout(message);
					setVisible(false);
				} else {
					System.out.println("取消退出");
				}
				
			}
		});
		userList.addListSelectionListener(new ListSelectionListener() {
			
			public void valueChanged(ListSelectionEvent e) {
				reciever = userList.getSelectedValue();
				setTitleContent(reciever);
				
			}
		});
		sendTf.addKeyListener(new KeyAdapter(){

			public void keyPressed(KeyEvent e) {

				if(e.getKeyChar()==KeyEvent.VK_ENTER){
					sendBtn.doClick();
				}
			}
		});
	}
	public static void main(String[] args) {
		new ChatFrame("evan");
	}
    
	public void setFriend(String name) {
		System.out.println("添加用户：" + name);	
		users.addElement(name);
	}
	public void setOnlineMessage(String name){
		String onlineMessage = "用户"+name+"上线啦！"+"   "+formatDate((new Date()).getTime());
		msgs.addElement(onlineMessage);
	}
	public void delUser(String name){
		for(int i = 0;i<users.getSize();i++){
			if(users.get(i).equals(name)){
				users.remove(i);
			}
		}
		autoFresh();
	}
	public void setDownlineMessage(String name){
		String downlineMessage = "用户"+name+"下线了！"+"   "+formatDate((new Date()).getTime());
		msgs.addElement(downlineMessage);
	}
	public void setShakeMessage(String name){
		String shakeMessage = "用户"+name+"向你发送了震屏操作..."+"   "+formatDate((new Date()).getTime());;
		msgs.addElement(shakeMessage);
	}
	public void setMessage(Message message){
		String allMessage = message.getSender()+"给所有人发送了一条信息"+"   "+formatDate(message.getTime());
		String selftMessage = message.getSender()+"发送了一条信息给"+message.getReciever()+"   "+formatDate(message.getTime());
		if("*".equals(message.getReciever())){
			msgs.addElement(allMessage);
		}else{
			msgs.addElement(selftMessage);
		}
		msgs.addElement(message.getContent());
	}
	public void actionPerformed(ActionEvent e) {
		if(e.getSource() == sendBtn){
			String content=sendTf.getText();
			if("".equals(content)){
				JOptionPane.showMessageDialog(null,
						"The message cannot be empty!");
			}else{
				Date date = new Date();
				Message message = new Message();
				message.setContent(content);
				message.setSender(name);
				message.setTime(date.getTime());
				message.setType(0);
				if(!"所有人".equals(reciever)){
					message.setReciever(reciever);
					Client.client.sendChat(message);
					String title = "我向"+reciever+"发送了一条消息"+"   "+formatDate(date.getTime());
					String messages = content;
					msgs.addElement(title);
					msgs.addElement(messages);
					System.out.println(name+"向"+reciever+"发送了"+content);
				}else{
					message.setReciever("*");
					Client.client.sendChat(message);
					System.out.println(name+"向所有人发送了"+content);
				}
				sendTf.setText("");
				autoFresh();
			}
			
		}else if(e.getSource() ==shakeBtn){
			Date date = new Date();
			Message message = new Message();
			message.setSender(name);;
			message.setTime(date.getTime());
			message.setType(1);
			Client.client.sendShake(message);
		}else if(e.getSource()==sendTf){
			sendTf.setText("");
		}
	}
	public void shake() {
		int x = this.getLocation().x;
		int y = this.getLocation().y;
		for (int i = 0; i < 10; i++) {
		try {
			Point endPoint = new Point(x + 10, y + 10);
			this.setLocation(endPoint);
			Thread.sleep(100);
			endPoint = new Point(x - 10, y - 10);
			setLocation(endPoint);
			Thread.sleep(100);
			} catch (InterruptedException e) {
			e.printStackTrace();
			}
		}
	}
	private void autoFresh(){
		try {
			Thread.sleep(100);
			msgJBar.setValue(msgJBar.getMaximum()); 
			userJBar.setValue(userJBar.getMaximum()); 
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	
	}
	private String formatDate(long time){
		String formatedTime = Constans.formatTime.format(new Date(time));
		return formatedTime;
	}
	private void setTitleContent(String reciever){
		this.setTitle("用户"+name+"正在跟"+reciever+"聊天");
	}

}
